<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript">
  $(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});
  
  var roleId=$("#roleId").val();
  
  var zTreeObj;
  // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
  var setting = {
          check: {
              enable: true,
              
              //nocheckInherit: false
              //chkStyle: 'checkbox',
              
//              chkboxType: { "Y": "ps", "N": "ps" }
          },
          view : {
                 selectedMulti: false,
                 showIcon: true,  //设置是否显示节点图标
                 showLine: true,  //设置是否显示节点与节点之间的连线
                // fontCss: setFontCss
             },
          async: {
              enable: true, // 设置 zTree是否开启异步加载模式  加载全部信息
              url: "", // Ajax 获取数据的 URL 地址  
              autoParam: ["id"],    // 异步加载时自动提交父节点属性的参数,假设父节点 node = {id:1, name:"test"}，异步加载时，提交参数 zId=1   
              //dataFilter: filter
          },
          callback: {
        	  onClick: zTreeOnClick,
              onCheck: zTreeOnCheck
          },
          data: { // 必须使用data  
              simpleData: {
                  enable: true,
                  idKey: "id", // id编号命名 默认  
                  pIdKey: "pid", // 父id编号命名 默认  
                  rootPId: 0 // 用于修正根节点父节点数据，即 pIdKey 指定的属性值  
              }
          }
  };
  
  //加载一级二级权限菜单
  $(function(){
	  $.ajax({
	        type: "POST",
	        url: rootPath +"/sysMenu/selectParentMenu.do",
	        data: {},
	        dataType:"json",
	        success: function(data){
	     	    zTreeObj = $.fn.zTree.init($("#treePermission"), setting, data);
	     	    var treeObj = $.fn.zTree.getZTreeObj("treePermission");
	            var nodes = treeObj.getNodes();
	            for (var i = 0; i < nodes.length; i++) { //设置节点展开
	                treeObj.expandNode(nodes[i], true, false, true);
	            }
	        },
	     });
  });
  
  //异步加载资源权限
  function zTreeOnClick(event, treeId, treeNode) {
	  var treeObj = $.fn.zTree.getZTreeObj(treeId);
	  var node = treeObj.getNodeByTId(treeNode.tId);
	  var sNodes = treeObj.getSelectedNodes();
	 //没有子节点才去查询
	if(sNodes[0].level<2){
		if(node.children == null || node.children == "undefined"||node.children==""){
			$.ajax({
		        type: "POST",
		        async:false,
		        url: rootPath +"/sysResource/selectSysResource.do",
		        data:{
					menuId:treeNode.id,roleId:roleId
				},
		        dataType:"json",
		        success: function(data){
					if(data !=null && data !=""){
						//添加新节点
						newNode = treeObj.addNodes(node, data);
					}
				},
		     });
		}
	}
	
  };
  var val = [];
  
  var checkCount=0;
  
  //选中事件
 function zTreeOnCheck(event, treeId, treeNode) {
	  var treeObj = $.fn.zTree.getZTreeObj(treeId);
	  var nodes = treeObj.getCheckedNodes(true);
	  for (var i=0;i<nodes.length;i++) {
		  if(nodes[i].isParent!=true){
// 			  v+=nodes[i].id+",";//获取选中节点的id值
			  val.push(nodes[i].id);
		  }
	   }
	  if(checkCount%2!=0){
		  val.splice(0,val.length);//清空数组
	  }
	  checkCount++;
 };
 
 
 $(function(){
	  xf.admin.menu${OP.menuId}.setPermission = function(obj) {
		$.ajax({
				type: "POST", 
				url: rootPath + "/op_setPermission_${OP.menuId}.do",
				data: {"resourceId":val.join(","),"roleId":roleId},
				dataType: "json",
				success: function(data){
					if(data.success) {
						xf.closeWindow();
						$('#admin_menu${OP.menuId}_datagrid').bootstrapTable('refresh');
					}
					xf.toastrsAlert({
		       		     code: data.success ? 'success' :'error',
		       		     msg: data.msg
		       		});
				}
			});
		}
 });
</script>

<div class="ibox float-e-margins animated fadeInRight">
	<div class="ibox-content">
		<form class="form-horizontal m-t required-validate"
			id="menu${OP.menuId}Form" action="op_update_${OP.menuId}.do"
			method="post">
			<input type="hidden" name="roleId" value="${dto.roleId}" id="roleId"/>
			<div class="form-group">
				<div class="col-sm-8">
				    <label class="col-sm-3 control-label">权限设置：</label>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-8">
			        <ul id="treePermission" class="ztree"></ul>
			    </div>
			</div>
		</form>
	</div>
</div>
